package cn.taobaoke.restful.service;


import cn.taobaoke.restful.domain.AgentGroupRelation;
import cn.taobaoke.restful.domain.AgentGroupRelationRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yangzuo
 * @since 2017-03-23
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AgentGroupRelationService {

    public AgentGroupRelation addRelationBetweenAgentAndGroup(final Long agentId, final Long groupId, final Long domainId) {
        val relationOpt = repository.findByAgentIdAndGroupIdAndDomainId(agentId, groupId, domainId);
        if (! relationOpt.isPresent()) {
            val relation = new AgentGroupRelation();
            relation.setAgentId(agentId);
            relation.setGroupId(groupId);
            relation.setDomainId(domainId);
            return repository.save(relation);
        }
        return null;
    }

    private final @NonNull AgentGroupRelationRepository repository;
}
